package com.wibo.account;


import java.util.ArrayList;
import java.util.HashMap;

import com.wibo.common.FileDbHelper;
import com.wibo.common.UserItem;
import com.wibo.common.Utility;

import weibo4android.IDs;
import weibo4android.User;
import weibo4android.Weibo;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;

public class CUserManager {
	private String TAG = "CUserManager";
	private Context mContext = null; 
	
	private static CUserManager sInstance;
	private static Weibo mWeibo = null;
	
	private User mUser = null;
	private UserItem mUserItem = null;
	private long mUserId = -1;
	
	private IDs mIds = null;
	
	private boolean mbLogined = false; 
	
	private static long mLastGetUnread = 0;
	
	private int mMySkin = -1;
	
//	private boolean mIsVerify = false;
//	public static 
	
	public static CUserManager getInstance(Context context) {
		if (sInstance == null) {
			sInstance = new CUserManager(context);
		}
		if (mWeibo == null){
    		mWeibo = OAuthConstant.getInstance().getWeibo();
    	}
		return sInstance;
	}
	
	public int getMySkin() {
		if (mMySkin < 0) {
			SharedPreferences settings = mContext.getSharedPreferences(/*WiboMainTabActivity.USER_INFO*/mContext.getPackageName() + "_preferences", 0);
			String skin = settings.getString(/*WiboMainTabActivity.USER_SKIN*/"skin_preference", "0");
			if (skin.equals("0")){
				mMySkin = 0;
			} else if (skin.equals("1")){
				mMySkin = 1;
			}else {
				mMySkin = 0;
			}
			
			return mMySkin;
		}else{
			return mMySkin;
		}
	}
	
	private int mTextSize = 0;
	public static final int SMALL = 0;
	public static final int MID = 1;
	public static final int LARGE = 2;
	public int getTextSize() {
		SharedPreferences settings = mContext.getSharedPreferences(mContext.getPackageName() + "_preferences", 0);
		String skin = settings.getString("testsize_preference", "small");
		if (skin.equals("small")){
			mTextSize = SMALL;
		} else if (skin.equals("mid")){
			mTextSize = MID;
		}else if (skin.equals("large")){
			mTextSize = LARGE;
		}else{
			mTextSize = SMALL;
		}
		
		return mTextSize;
	}
	
//	public void setMySkin(int newSkin) {
//		SharedPreferences settings = mContext.getSharedPreferences(mContext.getPackageName() + "_preferences", 0);
//		Editor editor = settings.edit();
//		editor.putInt("skin_preference", newSkin >= 0 ? newSkin : 0);
//		editor.commit();
//		mMySkin = newSkin;
//	}
	
	private CUserManager(Context mContext) {
		this.mContext = mContext;
	}
	
	public void setLastGetUnread(long time) {
		mLastGetUnread = time;
	}
	
	public long getLastGetUnread() {
		return mLastGetUnread;
	}
	
	public void setUserItem(UserItem userItem) {
		mUserItem = userItem;
	}
	
	public UserItem getUserItem() {
		return mUserItem;
	}
	
	public void setAccountUser(User user) {
		mUser = user;
		
		if (mUser != null) {
			mUserItem = new UserItem();
			mUserItem.setmFollowersCount(mUser.getFollowersCount());
			mUserItem.setmWeiboUserPic(mUser.getProfileImageURL().toString());
			mUserItem.setmScreenName(mUser.getScreenName());
			mUserItem.setmLocation(mUser.getLocation());
			mUserItem.setmDomain(mUser.getUserDomain());
			mUserItem.setmStatusesCount(mUser.getStatusesCount());
			mUserItem.setmFavouritesCount(mUser.getFavouritesCount());
			mUserItem.setmFriendsCount(mUser.getFriendsCount());
			mUserItem.setmUserName(mUser.getName());
			mUserItem.setmCreateTime(Utility.formatDate(mUser.getCreatedAt()));
			mUserItem.setmUrl(mUser.getURL() != null ? mUser.getURL().toString() : "");
			mUserItem.setmDescription(mUser.getDescription());
			mUserItem.setmUserId(mUser.getId());
			mUserItem.setmUserType(Utility.USER_MYSELF);
			mUserItem.setmVLogo(mUser.isVerified());
			mUserItem.setmSex(mUser.getGender());
		}
	}
	
	public User getAccountUser() {
		return mUser;
	}
	
	public void setUserId(long userId) {
		mUserId = userId;
	}
	
	public IDs getMyFocusIds() {
		return mIds;
	}
	
	public void setMyFocusIds(IDs ids) {
		mIds = ids;
	}
	
	public long getUserId() {
		if (mUser != null) {
			return mUser.getId();
		}else{
			if (mUserId > 0) {
				return mUserId;
			}else{
				SharedPreferences settings = mContext.getSharedPreferences(WiboMainTabActivity.USER_INFO, 0);
				return mUserId = settings.getLong(WiboMainTabActivity.USER_ID, -1);
			}
		}
	}
	
	public String getUserName() {
		SharedPreferences settings = mContext.getSharedPreferences(WiboMainTabActivity.USER_INFO, 0);
		return settings.getString(WiboMainTabActivity.USER_NAME, "");
	}
	
	private int mCommentMessage = 0;
	private int mAtMessage = 0;
	private int mTotal = 0;
	private int mFansCount = 0;
	private boolean mHaveUnread = false;
	
	public int getUnreadCommentCount() {
		return mCommentMessage;
	}
	
	public int getUnreadTransmitCount() {
		return mAtMessage;
	}
	
	public int getUnreadFansCount() {
		return mFansCount;
	}
	
	public int getUnreadTotalCount() {
		return mTotal;
	}
	
	public boolean getHaveUnread() {
		return mHaveUnread;
	}
	
	public void setUnreadCommentCount(int count) {
		mCommentMessage = count;
	}
	
	public void setUnreadTransmitCount(int count) {
		mAtMessage = count;
	}
	
	public void setUnreadFansCount(int count) {
		mFansCount = count;
	}
	
	public void setUnreadTotalCount(int count) {
		mTotal = count;
	}
	
	public void setHaveUnread(boolean isUnread) {
		mHaveUnread = isUnread;
	}
	
	public void insertUserToDB(FileDbHelper fileDbHelper, User user, int type) {
		try {
			fileDbHelper.open();
			
			HashMap<String, Object> hashMap = new HashMap<String, Object>();
			hashMap.put(FileDbHelper.USER_LIST_USER_ID, user
					.getId());
			hashMap.put(FileDbHelper.USER_LIST_USER_PIC, user
					.getProfileImageURL().toString());
			hashMap.put(FileDbHelper.USER_LIST_USERNAME, user
					.getName());
			hashMap.put(FileDbHelper.USER_LIST_FOLLOWERS_COUNT,
					(long)user.getFollowersCount());
			hashMap.put(FileDbHelper.USER_LIST_FRIENDS_COUNT, (long)user
					.getFriendsCount());
			hashMap.put(FileDbHelper.USER_LIST_FAVOURITES_COUNT, (long)user
					.getFavouritesCount());
			hashMap.put(FileDbHelper.USER_LIST_STATUSES_COUNT,
					(long)user.getStatusesCount());
			hashMap.put(FileDbHelper.USER_LIST_DOMAIN, user
					.getUserDomain());
			hashMap.put(FileDbHelper.USER_LIST_LOCATION, user
					.getLocation());
			
			hashMap.put(FileDbHelper.USER_LIST_SCREEN_NAME, user
					.getScreenName());
			
			hashMap.put(FileDbHelper.USER_LIST_VIP, user
					.isVerified() ? "true" : "false");

			
			hashMap.put(FileDbHelper.USER_LIST_URL, user
					.getURL().getPath());
			hashMap.put(FileDbHelper.USER_LIST_CREATE_TIME, Utility.formatDate(user
					.getCreatedAt()));
			hashMap.put(FileDbHelper.USER_LIST_DESCRIPTION, user
							.getDescription());
			hashMap.put(FileDbHelper.USER_LIST_ADD_DATA1, (long)type);
			hashMap.put(FileDbHelper.USER_LIST_ADD_DATA2, user.getGender());

			if (fileDbHelper.checkUserList(user.getId())){
				fileDbHelper.updateUserListData(hashMap);
			}else{
				fileDbHelper.insertUserListData(hashMap);
			}
		
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			fileDbHelper.close();
		}
	}
	
	public void insertUserToDB(FileDbHelper fileDbHelper, UserItem userItem) {
		try {
			fileDbHelper.open();
			
			HashMap<String, Object> hashMap = new HashMap<String, Object>();
			hashMap.put(FileDbHelper.USER_LIST_USER_ID, userItem
					.getmUserId());
			hashMap.put(FileDbHelper.USER_LIST_USER_PIC, userItem
					.getmWeiboUserPic());
			hashMap.put(FileDbHelper.USER_LIST_USERNAME, userItem
					.getmUserName());
			hashMap.put(FileDbHelper.USER_LIST_FOLLOWERS_COUNT,
					userItem.getmFollowersCount());
			hashMap.put(FileDbHelper.USER_LIST_FRIENDS_COUNT, userItem
					.getmFriendsCount());
			hashMap.put(FileDbHelper.USER_LIST_FAVOURITES_COUNT, userItem
					.getmFavouritesCount());
			hashMap.put(FileDbHelper.USER_LIST_STATUSES_COUNT,
					userItem.getmStatusesCount());
			hashMap.put(FileDbHelper.USER_LIST_DOMAIN, userItem
					.getmDomain());
			hashMap.put(FileDbHelper.USER_LIST_LOCATION, userItem
					.getmLocation());
			
			hashMap.put(FileDbHelper.USER_LIST_SCREEN_NAME, userItem
					.getmScreenName());
			
			hashMap.put(FileDbHelper.USER_LIST_VIP, userItem
					.ismVLogo() ? "true" : "false");

			
			hashMap.put(FileDbHelper.USER_LIST_URL, userItem
					.getmUrl());
			hashMap.put(FileDbHelper.USER_LIST_CREATE_TIME, userItem
					.getmCreateTime());
			hashMap.put(FileDbHelper.USER_LIST_DESCRIPTION, userItem
							.getmDescription());
			hashMap.put(FileDbHelper.USER_LIST_ADD_DATA1, userItem.getmUserType());

			hashMap.put(FileDbHelper.USER_LIST_ADD_DATA2, userItem.getmSex());
			fileDbHelper.insertUserListData(hashMap);
		
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			fileDbHelper.close();
		}
	}
	
	public UserItem getUserFromDB(FileDbHelper fileDbHelper, long userId, int userType){
		UserItem userItem = new UserItem();
		try {
			fileDbHelper.open();
			
			Cursor userCursor = fileDbHelper.getUserListData(userId);

			final int followers_count_id = userCursor
					.getColumnIndexOrThrow(FileDbHelper.USER_LIST_FOLLOWERS_COUNT);
			final int user_pic = userCursor
					.getColumnIndexOrThrow(FileDbHelper.USER_LIST_USER_PIC);
			final int screen_name = userCursor
					.getColumnIndexOrThrow(FileDbHelper.USER_LIST_SCREEN_NAME);
			final int location = userCursor
			.getColumnIndexOrThrow(FileDbHelper.USER_LIST_LOCATION);
			final int domain = userCursor
			.getColumnIndexOrThrow(FileDbHelper.USER_LIST_DOMAIN);
			final int statuses_count_pic = userCursor
					.getColumnIndexOrThrow(FileDbHelper.USER_LIST_STATUSES_COUNT);
			final int favourites_count = userCursor
			.getColumnIndexOrThrow(FileDbHelper.USER_LIST_FAVOURITES_COUNT);
			final int friends_count = userCursor
			.getColumnIndexOrThrow(FileDbHelper.USER_LIST_FRIENDS_COUNT);
			final int username = userCursor
					.getColumnIndexOrThrow(FileDbHelper.USER_LIST_USERNAME);
			final int create_time = userCursor
					.getColumnIndexOrThrow(FileDbHelper.USER_LIST_CREATE_TIME);
			final int url = userCursor
					.getColumnIndexOrThrow(FileDbHelper.USER_LIST_URL);
			final int description = userCursor
					.getColumnIndexOrThrow(FileDbHelper.USER_LIST_DESCRIPTION);
			
			final int user_id = userCursor
				.getColumnIndexOrThrow(FileDbHelper.USER_LIST_USER_ID);
			final int add_data1 = userCursor
				.getColumnIndexOrThrow(FileDbHelper.USER_LIST_ADD_DATA1);
			
			final int add_data2 = userCursor
			.getColumnIndexOrThrow(FileDbHelper.USER_LIST_ADD_DATA2);
			final int user_vip = userCursor
				.getColumnIndexOrThrow(FileDbHelper.USER_LIST_VIP);
			
			userCursor.moveToFirst();
			if (userCursor.getCount() == 1) {
				userItem.setmFollowersCount(userCursor.getLong(followers_count_id));
				userItem.setmWeiboUserPic(userCursor.getString(user_pic));
				userItem.setmScreenName(userCursor.getString(screen_name));
				userItem.setmLocation(userCursor.getString(location));
				userItem.setmDomain(userCursor.getString(domain));
				userItem.setmStatusesCount(userCursor
						.getLong(statuses_count_pic));
				userItem.setmFavouritesCount(userCursor.getLong(favourites_count));
				userItem.setmFriendsCount(userCursor.getLong(friends_count));
				userItem.setmSex(userCursor.getString(add_data2) == null ? "n" : userCursor.getString(add_data2));
				userItem.setmUserName(userCursor.getString(username));
				userItem.setmCreateTime(userCursor.getString(create_time));
				userItem.setmUrl(userCursor
						.getString(url));
				userItem.setmDescription(userCursor.getString(description));
				userItem.setmUserId(userCursor.getLong(user_id));
				userItem.setmUserType((int)userCursor.getLong(add_data1));
				userItem.setmVLogo(userCursor.getString(user_vip).equalsIgnoreCase("true"));
			}else{
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			fileDbHelper.close();
		}

		return userItem;
	}
	
	public ArrayList<UserItem> getUserListFromDB(FileDbHelper fileDbHelper, int userType){
		ArrayList<UserItem> userList = new ArrayList<UserItem>();
		
		try {
			fileDbHelper.open();
			
			Cursor userCursor = fileDbHelper.getAllUserListData();

			final int followers_count_id = userCursor
					.getColumnIndexOrThrow(FileDbHelper.USER_LIST_FOLLOWERS_COUNT);
			final int user_pic = userCursor
					.getColumnIndexOrThrow(FileDbHelper.USER_LIST_USER_PIC);
			final int screen_name = userCursor
					.getColumnIndexOrThrow(FileDbHelper.USER_LIST_SCREEN_NAME);
			final int location = userCursor
			.getColumnIndexOrThrow(FileDbHelper.USER_LIST_LOCATION);
			final int domain = userCursor
			.getColumnIndexOrThrow(FileDbHelper.USER_LIST_DOMAIN);
			final int statuses_count_pic = userCursor
					.getColumnIndexOrThrow(FileDbHelper.USER_LIST_STATUSES_COUNT);
			final int favourites_count = userCursor
			.getColumnIndexOrThrow(FileDbHelper.USER_LIST_FAVOURITES_COUNT);
			final int friends_count = userCursor
			.getColumnIndexOrThrow(FileDbHelper.USER_LIST_FRIENDS_COUNT);
			final int username = userCursor
					.getColumnIndexOrThrow(FileDbHelper.USER_LIST_USERNAME);
			final int create_time = userCursor
					.getColumnIndexOrThrow(FileDbHelper.USER_LIST_CREATE_TIME);
			final int url = userCursor
					.getColumnIndexOrThrow(FileDbHelper.USER_LIST_URL);
			final int description = userCursor
					.getColumnIndexOrThrow(FileDbHelper.USER_LIST_DESCRIPTION);
			
			final int user_id = userCursor
				.getColumnIndexOrThrow(FileDbHelper.USER_LIST_USER_ID);
			final int add_data1 = userCursor
				.getColumnIndexOrThrow(FileDbHelper.USER_LIST_ADD_DATA1);
			
			final int add_data2 = userCursor
			.getColumnIndexOrThrow(FileDbHelper.USER_LIST_ADD_DATA2);
			final int user_vip = userCursor
				.getColumnIndexOrThrow(FileDbHelper.USER_LIST_VIP);
			
			userCursor.moveToLast();
			for (int i = userCursor.getCount() - 1; i >= 0; i--){
				UserItem userItem = new UserItem();
				if (userType != (int)userCursor.getLong(add_data1) && userType >= 0){
					userCursor.moveToPrevious();
					continue;
				}
				
				userItem.setmFollowersCount(userCursor.getLong(followers_count_id));
				userItem.setmWeiboUserPic(userCursor.getString(user_pic));
				userItem.setmScreenName(userCursor.getString(screen_name));
				userItem.setmLocation(userCursor.getString(location));
				userItem.setmDomain(userCursor.getString(domain));
				userItem.setmStatusesCount(userCursor
						.getLong(statuses_count_pic));
				userItem.setmFavouritesCount(userCursor.getLong(favourites_count));
				userItem.setmFriendsCount(userCursor.getLong(friends_count));
				userItem.setmUserName(userCursor.getString(username));
				userItem.setmCreateTime(userCursor.getString(create_time));
				userItem.setmUrl(userCursor
						.getString(url));
				userItem.setmDescription(userCursor.getString(description));
				userItem.setmUserId(userCursor.getLong(user_id));
				userItem.setmUserType((int)userCursor.getLong(add_data1));
				userItem.setmVLogo(userCursor.getString(user_vip).equalsIgnoreCase("true"));
				userItem.setmSex(userCursor.getString(add_data2) == null ? "n" : userCursor.getString(add_data2));
				
				userList.add(userItem);
				userCursor.moveToPrevious();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			fileDbHelper.close();
		}
		
		return userList;
	}
}
